package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"hospital/models"
	"strconv"
	"time"
)

type PatientController struct {
	beego.Controller
}

func (c PatientController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 10)
	var count int64
	query := models.DB.Model(&models.Patient{}).Count(&count)
	key := "patient-" + strconv.Itoa(page)
	val, err := models.Rdb.Get(key).Result()
	pageData := make(map[string]interface{})
	if err != nil {
		offset := (page - 1) * size
		var patient []models.Patient
		err = query.Offset(offset).Limit(size).Find(&patient).Error
		pageData["count"] = count
		pageData["patient"] = patient
		if err != nil {
			c.Data["json"] = JsonR{
				Conde: 500,
				Msg:   "数据查询失败",
				Data:  nil,
			}
			c.ServeJSON()
			return
		} else {
			marshal, _ := json.Marshal(&pageData)
			value := string(marshal)
			models.Rdb.Set(key, value, time.Second*10)
			c.Data["json"] = JsonR{
				Conde: 200,
				Msg:   "数据查询成功-mysql",
				Data:  pageData,
			}
			c.ServeJSON()
			return
		}
	} else {
		json.Unmarshal([]byte(val), &pageData)
		c.Data["json"] = JsonR{
			Conde: 200,
			Msg:   "数据查询成功-redis",
			Data:  pageData,
		}
		c.ServeJSON()
		return
	}

	/*	page, _ := c.GetInt("page", 1)
		size, _ := c.GetInt("size", 10)
		name := c.GetString("name")
		where := make(map[string]string)
		if name != "" {
			where["name"] = name
		}
		key := "patient-" + strconv.Itoa(page)
		val, err := models.Rdb.Get(key).Result()
		pageData := make(map[string]interface{})
		if err != nil {
			var count int64
			models.DB.Model(&models.Patient{}).Count(&count)
			offset := (page - 1) * size
			var patient []models.Patient
			err = models.DB.Debug().Offset(offset).Limit(size).Find(&patient).Error
			pageData["count"] = count
			pageData["patient"] = patient
			if err != nil {
				c.Data["json"] = JsonR{
					Conde: 500,
					Msg:   "数据查询失败",
					Data:  nil,
				}
				c.ServeJSON()
				return
			} else {
				marshal, _ := json.Marshal(&pageData)
				value := string(marshal)
				models.Rdb.Set(key, value, time.Second*10)
				if err != nil {
					c.Data["json"] = JsonR{
						Conde: 200,
						Msg:   "mysql查询成功",
						Data:  pageData,
					}
					c.ServeJSON()
				}
			}
		} else {
			json.Unmarshal([]byte(val), &pageData)
			c.Data["json"] = JsonR{
				Conde: 200,
				Msg:   "redis查询成功",
				Data:  pageData,
			}
			c.ServeJSON()
			return
		}*/
}

func (c *PatientController) Add() {
	name := c.GetString("name")
	num, _ := c.GetInt("num")
	esx := c.GetString("esx")
	age, _ := c.GetInt("age")
	time := c.GetString("time")
	img := c.GetString("img")
	status := c.GetString("status")
	if name == "" || num == 0 || esx == "" || age == 0 || time == "" || img == "" {
		c.Data["json"] = JsonR{
			Conde: 500,
			Msg:   "数据不完整",
			Data:  nil,
		}
		c.ServeJSON()
		return
	}
	patient := models.Patient{
		Name:   name,
		Num:    num,
		Esx:    esx,
		Age:    age,
		Time:   time,
		Img:    img,
		Status: status,
	}
	err := models.DB.Create(&patient).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Conde: 500,
			Msg:   "数据添加失败",
			Data:  nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Conde: 200,
			Msg:   "数据填加成功",
			Data:  patient,
		}
		c.ServeJSON()
		return
	}
}

func (c *PatientController) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = JsonR{
			Conde: 500,
			Msg:   "请上传文件",
			Data:  nil,
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	if header.Size > 5*1024*1024 {
		c.Data["json"] = JsonR{
			Conde: 500,
			Msg:   "文件超过5MB",
			Data:  nil,
		}
		c.ServeJSON()
		return
	}
	path := "static/img/" + header.Filename
	err = c.SaveToFile("img", path)
	if err != nil {
		err = models.DB.Model(&models.Patient{}).Create(&path).Error
		c.Data["json"] = JsonR{
			Conde: 500,
			Msg:   "文件上传失败",
			Data:  nil,
		}
		c.ServeJSON()
		return
	} else {
		img := "http://127.0.0.1:8080/" + path
		c.Data["json"] = JsonR{
			Conde: 200,
			Msg:   "文件上传成功",
			Data:  img,
		}
		c.ServeJSON()
		return
	}
}

func (c *PatientController) Info() {
	id, _ := c.GetInt("id")
	key := "patient-" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()
	var patient []models.Patient
	if err != nil {
		err = models.DB.Model(&models.Patient{}).Where("ID = ?", id).First(&patient).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Conde: 500,
				Msg:   "数据详情查询失败",
				Data:  nil,
			}
			c.ServeJSON()
			return
		} else {
			marshal, _ := json.Marshal(&patient)
			value := string(marshal)
			models.Rdb.Set(key, value, time.Second*10)
			c.Data["json"] = JsonR{
				Conde: 200,
				Msg:   "mysql数据详情查询成功",
				Data:  patient,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &patient)
		c.Data["json"] = JsonR{
			Conde: 200,
			Msg:   "redis数据详情查询成功",
			Data:  patient,
		}
		c.ServeJSON()
	}
}
